/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.animation.interaction.scenetransition;

import com.bytedance.scene.animation.interaction.progressanimation.AnimatorFactory;
import com.bytedance.scene.animation.interaction.progressanimation.InteractionAnimation;
import com.bytedance.scene.animation.interaction.scenetransition.utils.ArgbEvaluator;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.utils.ObjectAttribute;

public class BackgroundRecolor extends SceneTransition {
    private ArgbEvaluator mIntEvaluator = new ArgbEvaluator();
    private int mStartColor;
    private int mEndColor;

    public BackgroundRecolor() {
    }

    @Override
    public void captureValue(Component fromView, Component toView, Component animationView) {
        super.captureValue(fromView, toView, animationView);
        if (fromView.getBackgroundElement() instanceof ShapeElement && toView.getBackgroundElement() instanceof ShapeElement && animationView.getBackgroundElement() instanceof ShapeElement) {
            int length = ((ShapeElement) this.mFromView.getBackgroundElement()).getRgbColors().length;
            mStartColor = ((ShapeElement) this.mFromView.getBackgroundElement()).getRgbColors()[0].asRgbaInt();
            mEndColor = ((ShapeElement) this.mToView.getBackgroundElement()).getRgbColors()[length - 1].asRgbaInt();
        }
    }

    @Override
    public InteractionAnimation getAnimation(boolean push) {
        if (mStartColor != mEndColor) {
            final ShapeElement endColorDrawable = (ShapeElement) mAnimationView.getBackgroundElement();
            mAnimationView.setBackground(endColorDrawable);
            endColorDrawable.setRgbColor(RgbColor.fromArgbInt(mStartColor));

            AnimatorFactory<ShapeElement, Integer> animatorFactory = new AnimatorFactory<>(endColorDrawable,
                    new ObjectAttribute<ShapeElement, Integer>(Integer.class, "backgroundColor") {
                        @Override
                        public void set(ShapeElement object, Integer value) {
                            object.setRgbColor(RgbColor.fromArgbInt(value));
                        }

                        @Override
                        public Integer get(ShapeElement object) {
                            return object.getRgbColors()[0].asArgbInt();
                        }
                    }, ArgbEvaluator.getInstance(), mStartColor, mEndColor);
            return animatorFactory.toInteractionAnimation();
        }
        return InteractionAnimation.EMPTY;
    }

    @Override
    public void finish(boolean push) {

    }
}